package com.hr.controller;

import com.hr.dto.JsonResp;
import com.hr.model.*;
import com.hr.service.*;
import com.hr.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hr")
public class HrController
{
    @Autowired
    DepartmentService departmentService;
    @Autowired
    WantedService wantedService;
    @Autowired
    ArchiveService archiveService;
    @Autowired
    CompanyService companyService;
    @Autowired
    PositionService positionService;
    @Autowired
    EvaluateService evaluateService;

    @RequestMapping(value = "/per/get",method = RequestMethod.GET)
    ResponseEntity<JsonResp> getDepartments(@RequestParam int cid)
    {
        JsonResp jsonResp;
        List<Department> departments=departmentService.getDepartmentsByCId(cid);
        if(departments.size()==0)
            jsonResp=JsonResp.ok("尚无部门");
        else
            jsonResp=JsonResp.ok("departments",departments);
        return new ResponseEntity<>(jsonResp, HttpStatus.OK);
    }



    @RequestMapping(value = "/mar/get", method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getPublicArchives()
    {
        return new ResponseEntity<>(JsonResp.ok("archives", archiveService.getPublicArchives()), HttpStatus.OK);
    }

    @RequestMapping(value = "/mng/get", method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getWantedByCompany(@RequestParam("cid") int cid)
    {
        return new ResponseEntity<>(JsonResp.ok("wanteds",wantedService.getWantedsByCId(cid)), HttpStatus.OK);
    }

    @RequestMapping(value = "/mng/status/get", method = RequestMethod.GET)
    public ResponseEntity<JsonResp> getWantedStatus(@RequestParam("wid") int wid,@RequestParam("cid") int cid)
    {
        return new ResponseEntity<>(JsonResp.ok("U_Ws",wantedService.getUWsByWId(wid)), HttpStatus.OK);
    }

    @RequestMapping(value = "/mng/post", method = RequestMethod.POST)
    public ResponseEntity<JsonResp> postWanted(@RequestParam int cid,@RequestBody Wanted wanted)
    {

        Company company=companyService.getCompanyByCId(wanted.getCompany().getId());
        Department department=departmentService.getDepartmentByDepId(wanted.getDepartment().getId());
        if(company==null)
            return new ResponseEntity<>(JsonResp.badRequest("公司id不存在"), HttpStatus.BAD_REQUEST);
        else if(department==null)
            return new ResponseEntity<>(JsonResp.badRequest("部门id不存在"), HttpStatus.BAD_REQUEST);
        else
        {
            boolean flag=false;

            if(company.getDepartments().size()==0||wanted.getCompany()==null)
                return new ResponseEntity<>(JsonResp.badRequest("部门不属于该公司或不存在"), HttpStatus.BAD_REQUEST);

            flag=false;
            for(Department department1: company.getDepartments())
            {
                if(department1.getId()==department.getId())
                    flag=true;
            }
            if(!flag)
                return new ResponseEntity<>(JsonResp.badRequest("部门不属于该公司或不存在"), HttpStatus.BAD_REQUEST);
        }

        wantedService.insertWanted(wanted);
        return new ResponseEntity<>(JsonResp.ok("发布成功","wid",wanted.getId()), HttpStatus.OK);
    }

    @RequestMapping(value = "/mng/put", method = RequestMethod.PUT)
    public ResponseEntity<JsonResp> checktWanted(@RequestParam int cid,@RequestParam int wid)
    {
        if(!wantedService.isHrOfWanted(wid))
            return new ResponseEntity<>(JsonResp.badRequest("部门不属于该公司或不存在或没有管理权限"), HttpStatus.BAD_REQUEST);

        Wanted wanted=wantedService.getWantedByWId(wid);


        wantedService.checkWanted(wid,!wanted.isEnable());
        if(wanted.isEnable())
            return new ResponseEntity<>(JsonResp.ok("关闭招聘成功"), HttpStatus.OK);
        else
            return new ResponseEntity<>(JsonResp.ok("开启招聘成功"), HttpStatus.OK);

    }

    @RequestMapping(value = "/per/put", method = RequestMethod.PUT)
    public ResponseEntity<JsonResp> putPosition(@RequestParam int cid,@RequestBody Position position)
    {
        Position oldPosition=positionService.getPositionByPId(position.getId());

        if(oldPosition.getCompany().getId()!=cid)
            return new ResponseEntity<>(JsonResp.badRequest("非本公司的职位"), HttpStatus.BAD_REQUEST);

        if(!departmentService.ifDepartmentBelong2Company(position.getDepartment().getId(),cid))
            return new ResponseEntity<>(JsonResp.badRequest("非本公司的部门"), HttpStatus.BAD_REQUEST);

        positionService.updatePosition(position);

        return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
    }

    @RequestMapping(value = "/per/dep/post", method = RequestMethod.POST)
    public ResponseEntity<JsonResp> postDepartmnet(@RequestParam int cid,@RequestBody Department department)
    {
        departmentService.insertDepartment(cid, department);
        return new ResponseEntity<>(JsonResp.ok("新增成功","depid",department.getId()), HttpStatus.OK);
    }

    @RequestMapping(value = "/per/dep/put", method = RequestMethod.PUT)
    public ResponseEntity<JsonResp> putDepartmnet(@RequestParam int cid,@RequestBody Department department)
    {
        if(!departmentService.ifDepartmentBelong2Company(department.getId(),cid))
            return new ResponseEntity<>(JsonResp.badRequest("非本公司的部门"), HttpStatus.BAD_REQUEST);

        departmentService.updateDepartment(department);
        return new ResponseEntity<>(JsonResp.ok("修改成功"), HttpStatus.OK);
    }

    @RequestMapping(value = "/per/dep/delete", method = RequestMethod.DELETE)
    public ResponseEntity<JsonResp> deleteDepartmnet(@RequestParam int cid,@RequestParam int depid)
    {
        if(!departmentService.ifDepartmentBelong2Company(depid,cid))
            return new ResponseEntity<>(JsonResp.badRequest("非本公司的部门"), HttpStatus.BAD_REQUEST);

        departmentService.deleteDepartment(depid);
        return new ResponseEntity<>(JsonResp.ok("删除成功"), HttpStatus.OK);
    }

    @RequestMapping(value = "/per/eva/post", method = RequestMethod.POST)
    public ResponseEntity<JsonResp> postEvaluation(@RequestParam int cid,@RequestParam int aid,@RequestBody Evaluate evaluate)
    {
        evaluate.setCommenter(AuthUtil.getCurUser());
        evaluate.setDate(new Date());
        evaluateService.insertEvaluation(aid,evaluate);

        return new ResponseEntity<>(JsonResp.ok("评价成功","eid",evaluate.getId()), HttpStatus.OK);
    }

    @RequestMapping(value = "/per/eva/put", method = RequestMethod.PUT)
    public ResponseEntity<JsonResp> putEvaluation(@RequestParam int cid,@RequestBody Evaluate evaluate)
    {
        Evaluate oldEvaluate=evaluateService.getEvaluateByEId(evaluate.getId());

        if(oldEvaluate==null||oldEvaluate.getCommenter().getId()!=AuthUtil.getCurUser().getId())
            return new ResponseEntity<>(JsonResp.badRequest("改评价属于其他用户或不存在"), HttpStatus.BAD_REQUEST);

        evaluateService.updateEvaluation(evaluate);
        return new ResponseEntity<>(JsonResp.ok("修改评价成功"), HttpStatus.OK);
    }

    @RequestMapping(value = "/per/eva/delete", method = RequestMethod.DELETE)
    public ResponseEntity<JsonResp> deleteEvaluation(@RequestParam int cid,@RequestParam int eid)
    {
        Evaluate oldEvaluate=evaluateService.getEvaluateByEId(eid);

        if(oldEvaluate==null||oldEvaluate.getCommenter()==null||oldEvaluate.getCommenter().getId()!=AuthUtil.getCurUser().getId())
            return new ResponseEntity<>(JsonResp.badRequest("该评价属于其他用户或不存在"), HttpStatus.BAD_REQUEST);

        evaluateService.deleteEvaluation(eid);
        return new ResponseEntity<>(JsonResp.ok("删除评价成功"), HttpStatus.OK);
    }

}
